package com.tengju.bff.interfaces.shared;

import com.tengju.data.domain.model.analysis.MetaType;
import com.tengju.data.domain.shared.CastUtil;
import lombok.Getter;

import java.math.BigDecimal;
import java.util.Set;

/**
 * @author chenhang
 * @Date 2021/1/26
 */
@Getter
public enum AmountType implements AmountFormat {
    /**
     * 百位抹0，千分位展示，大于1000万时返回1000W+
     */
    INDEX(Set.of()) {
        @Override
        public String format(Object amount) {
            if (amount instanceof BigDecimal) {
                return Formatter.formatIndex(((BigDecimal) amount).intValue());
            }
            if (amount instanceof Integer) {
                return Formatter.formatIndex((Integer) amount);
            }
            throw new FormatException("不支持的转换类型");
        }
    },

    /**
     * 百分比，保留两位小数
     */
    PERCENT(Set.of(MetaType.Percent)) {
        @Override
        public String format(Object amount) {
            if (amount instanceof Double) {

                return Formatter.percent((Double) amount, 2);
            }
            if (amount instanceof BigDecimal) {

                return Formatter.percent(((BigDecimal)amount).doubleValue(), 2);
            }
            return "-";
        }
    },

    /**
     * 百分比，绩效目标
     */
    TARGET_PERCENT(Set.of(MetaType.TargetPercent)) {
        @Override
        public String format(Object amount) {
            if (amount instanceof Double) {
                if ((Double) amount < 0) {
                    return "-";
                }
                return Formatter.percent((Double) amount, 2);
            }
            if (amount instanceof BigDecimal) {
                if (((BigDecimal)amount).compareTo(BigDecimal.ZERO) < 0) {
                    return "-";
                }
                return Formatter.percent(((BigDecimal)amount).doubleValue(), 2);
            }
            return "-";
        }
    },

    /**
     * 百分比，不保留小数
     */
    PERCENT_0(Set.of()) {
        @Override
        public String format(Object amount) {
            if (amount == null){
                return "-";
            }
            if (amount instanceof Double) {
                return Formatter.percent((Double) amount, 0);
            }
            if (amount instanceof BigDecimal) {
                return Formatter.percent(((BigDecimal) amount).doubleValue(), 0);
            }
            return null;
        }
    },

    /**
     * 千分位，数字全部展示
     */
    THOUSAND(Set.of(MetaType.Thousand)) {
        @Override
        public String format(Object amount) {

            return Formatter.thousandObj(amount);
        }
    },


    NONE(Set.of(MetaType.None,MetaType.Name,MetaType.Number,MetaType.String)) {
        @Override
        public String format(Object amount) {
            if (amount instanceof Double) {
                return String.valueOf(BigDecimal.valueOf((Double) amount));
            }
            return amount == null ? "-" : String.valueOf(amount);
        }
    },

    DATE(Set.of(MetaType.Date)) {
        @Override
        public String format(Object amount) {
            return CastUtil.castString(Formatter.formatterDate(amount));
        }

    },

    DATETIME(Set.of(MetaType.DateTime)) {
        @Override
        public String format(Object amount) {
            return CastUtil.castString(Formatter.formatterDateTime(amount));
        }

    },

    TEN_MILLION(Set.of(MetaType.TenMillion)) {
        @Override
        public String format(Object amount) {
            return CastUtil.castString(Formatter.formatObjOfTenMillion(amount));
        }
    },

    /**
     * 单位万千分位展示
     * eg:2,345万   3,345
     */
    TEN_THOUSAND_MONEY(Set.of(MetaType.TenThousandMoney)) {
        @Override
        public String format(Object amount) {
            return Formatter.tenThousandMoney(amount);
        }
    },

    /**
     * 千分位，数字全部展示, 并保留两位小数
     */
    NUMBER_2(Set.of(MetaType.Number_2)) {
        @Override
        public String format(Object amount) {
            return Formatter.numberFormat(amount,"###,###.##");
        }
    },

    /**
     * 千分位，数字全部展示, 并保留两位小数
     */
    Normal_Number_2(Set.of(MetaType.Normal_Number_2)) {
        @Override
        public String format(Object amount) {
            return Formatter.numberFormat(amount,"###.##");
        }
    },

    Normal_Number_4(Set.of(MetaType.Normal_Number_4)) {
        @Override
        public String format(Object amount) {
            return Formatter.numberFormat(amount,"###.####");
        }
    },


    ;

    Set<MetaType> bindMetas;

    AmountType(Set<MetaType> bindMetas) {
        this.bindMetas = bindMetas;
    }

    public static AmountType parseByMeta(MetaType metaType) {
        if (metaType == null) {
            return NONE;
        }
        for (AmountType amountType : values()) {
            if (amountType.bindMetas.contains(metaType)) {
                return amountType;
            }
        }
        return NONE;
    }


    public static void main(String[] args) {
        System.out.println(Formatter.numberFormat(1212323434234.343,"###.##"));
    }
}
